package auctionhaus

import grails.test.ControllerUnitTestCase
import grails.test.mixin.TestFor
import org.springframework.mock.web.MockHttpServletRequest
import grails.converters.JSON

class ListingControllerTests extends ControllerUnitTestCase {

    public testLoadLatestBids()
    {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.save(failOnError: true)
        def bob = new Customer(emailAddress: 'bob@umn.edu', password: 'password')
        bob.save(failOnError: true)
        def carl = new Customer(emailAddress: 'carl@umn.edu', password: 'password')
        carl.save(failOnError: true)

        def listing = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100)
        listing.bids = new HashSet<Bid>()
        listing.save(failOnError: true, flush: true)

        def bid = new Bid(bidder: bob, listing: listing , amount: 103)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: carl, listing: listing , amount: 104)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 106)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 107)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: carl, listing: listing , amount: 108)
        bid.save(failOnError: true, flush: true)

        Map myParams = [id: listing.id]
        controller.params.putAll(myParams)
        controller.getLatestBids();

        def json = controller.response.text
        def jsonMap = JSON.parse(json)
        assert jsonMap.bids.size() == 5
        assert jsonMap.bids.first().amount == 108
        assert jsonMap.bids.first().bidder == 'carl'
    }


    public testLoadLatestBidsMoreThan10()
    {
        def alice = new Customer(emailAddress: 'alice@umn.edu', password: 'password')
        alice.save(failOnError: true)
        def bob = new Customer(emailAddress: 'bob@umn.edu', password: 'password')
        bob.save(failOnError: true)
        def carl = new Customer(emailAddress: 'carl@umn.edu', password: 'password')
        carl.save(failOnError: true)

        def listing = new Listing(name: 'bicycle', endDateTime: new Date() + 5, seller: alice, startingBidAmount: 100)
        listing.bids = new HashSet<Bid>()
        listing.save(failOnError: true, flush: true)

        def bid = new Bid(bidder: bob, listing: listing , amount: 103)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: carl, listing: listing , amount: 104)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 106)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 107)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: carl, listing: listing , amount: 108)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 109)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: carl, listing: listing , amount: 111)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 112)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: carl, listing: listing , amount: 113)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 114)
        bid.save(failOnError: true, flush: true)
        bid = new Bid(bidder: bob, listing: listing , amount: 115)
        bid.save(failOnError: true, flush: true)

        Map myParams = [id: listing.id]
        controller.params.putAll(myParams)
        controller.getLatestBids();

        def json = controller.response.text
        def jsonMap = JSON.parse(json)
        assert jsonMap.bids.size() == 10
        assert jsonMap.bids.first().amount == 115
        assert jsonMap.bids.first().bidder == 'bob'
    }

}
